'use strict';
const { webviewTag } = require('electron');
let electron = require('electron')  //引入electron模块
var robot = require("robotjs");
var a, b, c, d, e, f, g, h, i, j, k
var judge_arr1, judge_arr2, judge_arr3, judge_arr4, judge_arr5, judge_arr6, judge_arr7, judge_arr8, judge_arr9, judge_arr10
var KeyIndex = 0
var NextKey = undefined
const hook = require('iohook')
hook.on('keydown', (msg) => {
  KeyIndex++
  NextKey = msg.rawcode
})
hook.start();
async function WaitKey() {
  return await new Promise((resolve, reject) => {
    let LastKeyIndex = KeyIndex
    let timer = setInterval(() => {
      if (KeyIndex > LastKeyIndex) {
        clearInterval(timer)
        resolve(NextKey)
      }
    }, 10)
  })
}

bj1()

async function bj1() {
  a = await WaitKey()
  await judge()
  b = await WaitKey()
  await judge()
  c = await WaitKey()
  await judge()
  d = await WaitKey()
  await judge()
  e = await WaitKey()
  await judge()
  f = await WaitKey()
  await judge()
  g = await WaitKey()
  await judge()
  h = await WaitKey()
  await judge()
  i = await WaitKey()
  await judge()
  j = await WaitKey()
  await judge()
  k = await WaitKey()
  await judge()
  bj1()
}


async function judge() {
  await is32()
}
async function is32() {
  if (a == 32) {
    GeshihuaA()
    await BeginThreads()
  }
  if (b == 32) {
    GeshihuaB()
    await BeginThreads()
  }
  if (c == 32) {
    GeshihuaC()
    await BeginThreads()
  }
  if (d == 32) {
    GeshihuaD()
    await BeginThreads()
  }
  if (e == 32) {
    GeshihuaE()
    await BeginThreads()
  }
  if (f == 32) {
    GeshihuaF()
    await BeginThreads()
  }
  if (g == 32) {
    GeshihuaG()
    await BeginThreads()
  }
  if (h == 32) {
    GeshihuaH()
    await BeginThreads()
  }
  if (i == 32) {
    GeshihuaI()
    await BeginThreads()
  }
  if (g == 32) {
    GeshihuaJ()
    await BeginThreads()
  }
  if (k == 32) {
    GeshihuaK()
    await BeginThreads()
  }
}
async function BeginThreads() {
  await FirstJudge()
  // TenthJudge()
  // NinthJudge()
  // EighthJudge()
  // SeventhJudge()
  // SixthJudge()
  // FifthJudge()
  // FourthJudge()
  // ThirdJudge()
  // SecondJudge()
}
async function FirstJudge() {
  await print(':')
}
async function print(what) {
  console.log('judge_arr1', judge_arr1);
  if (judge_arr1 === what) {
    console.log(what)
    let c = await WaitKey()
    if (c == 32) {
      hook.stop()
      robot.typeString(what)
      hook.start()
      ResetVars()
      bj1()
      return
    }
  }
}
function ResetVars() {//给所有的值转换为初始状态
  a = undefined
  b = undefined
  c = undefined
  d = undefined
  e = undefined
  f = undefined
  g = undefined
  h = undefined
  i = undefined
  j = undefined
  k = undefined
  judge_arr1 = undefined
  judge_arr2 = undefined
  judge_arr3 = undefined
  judge_arr4 = undefined
  judge_arr5 = undefined
  judge_arr6 = undefined
  judge_arr7 = undefined
  judge_arr8 = undefined
  judge_arr9 = undefined
  judge_arr10 = undefined
}
function GeshihuaA() {
  judge_arr1 = PressedChar(k)
  judge_arr2 = PressedChar(j) + judge_arr1
  judge_arr3 = PressedChar(i) + judge_arr2
  judge_arr4 = PressedChar(h) + judge_arr3
  judge_arr5 = PressedChar(g) + judge_arr4
  judge_arr6 = PressedChar(f) + judge_arr5
  judge_arr7 = PressedChar(e) + judge_arr6
  judge_arr8 = PressedChar(d) + judge_arr7
  judge_arr9 = PressedChar(c) + judge_arr8
  judge_arr10 = PressedChar(b) + judge_arr9
}
function GeshihuaB() {
  judge_arr1 = PressedChar(a)
  judge_arr2 = PressedChar(k) + judge_arr1
  judge_arr3 = PressedChar(j) + judge_arr2
  judge_arr4 = PressedChar(i) + judge_arr3
  judge_arr5 = PressedChar(h) + judge_arr4
  judge_arr6 = PressedChar(g) + judge_arr5
  judge_arr7 = PressedChar(f) + judge_arr6
  judge_arr8 = PressedChar(e) + judge_arr7
  judge_arr9 = PressedChar(d) + judge_arr8
  judge_arr10 = PressedChar(c) + judge_arr9
}
function GeshihuaC() {
  judge_arr1 = PressedChar(b)
  judge_arr2 = PressedChar(a) + judge_arr1
  judge_arr3 = PressedChar(k) + judge_arr2
  judge_arr4 = PressedChar(j) + judge_arr3
  judge_arr5 = PressedChar(i) + judge_arr4
  judge_arr6 = PressedChar(h) + judge_arr5
  judge_arr7 = PressedChar(g) + judge_arr6
  judge_arr8 = PressedChar(f) + judge_arr7
  judge_arr9 = PressedChar(e) + judge_arr8
  judge_arr10 = PressedChar(d) + judge_arr9
}
function GeshihuaD() {
  judge_arr1 = PressedChar(c)

  judge_arr2 = PressedChar(b) + judge_arr1
  judge_arr3 = PressedChar(a) + judge_arr2
  judge_arr4 = PressedChar(k) + judge_arr3
  judge_arr5 = PressedChar(j) + judge_arr4
  judge_arr6 = PressedChar(i) + judge_arr5
  judge_arr7 = PressedChar(h) + judge_arr6
  judge_arr8 = PressedChar(g) + judge_arr7
  judge_arr9 = PressedChar(f) + judge_arr8
  judge_arr10 = PressedChar(e) + judge_arr9
}
function GeshihuaE() {
  judge_arr1 = PressedChar(d)
  judge_arr2 = PressedChar(c) + judge_arr1
  judge_arr3 = PressedChar(b) + judge_arr2
  judge_arr4 = PressedChar(a) + judge_arr3
  judge_arr5 = PressedChar(k) + judge_arr4
  judge_arr6 = PressedChar(j) + judge_arr5
  judge_arr7 = PressedChar(i) + judge_arr6
  judge_arr8 = PressedChar(h) + judge_arr7
  judge_arr9 = PressedChar(g) + judge_arr8
  judge_arr10 = PressedChar(f) + judge_arr9
}
function GeshihuaF() {
  judge_arr1 = PressedChar(e)
  judge_arr2 = PressedChar(d) + judge_arr1
  judge_arr3 = PressedChar(c) + judge_arr2
  judge_arr4 = PressedChar(b) + judge_arr3
  judge_arr5 = PressedChar(a) + judge_arr4
  judge_arr6 = PressedChar(k) + judge_arr5
  judge_arr7 = PressedChar(j) + judge_arr6
  judge_arr8 = PressedChar(i) + judge_arr7
  judge_arr9 = PressedChar(h) + judge_arr8
  judge_arr10 = PressedChar(g) + judge_arr9
}
function GeshihuaG() {
  judge_arr1 = PressedChar(f)
  judge_arr2 = PressedChar(e) + judge_arr1
  judge_arr3 = PressedChar(d) + judge_arr2
  judge_arr4 = PressedChar(c) + judge_arr3
  judge_arr5 = PressedChar(b) + judge_arr4
  judge_arr6 = PressedChar(a) + judge_arr5
  judge_arr7 = PressedChar(k) + judge_arr6
  judge_arr8 = PressedChar(j) + judge_arr7
  judge_arr9 = PressedChar(i) + judge_arr8
  judge_arr10 = PressedChar(h) + judge_arr9
}
function GeshihuaH() {
  judge_arr1 = PressedChar(g)
  judge_arr2 = PressedChar(f) + judge_arr1
  judge_arr3 = PressedChar(e) + judge_arr2
  judge_arr4 = PressedChar(d) + judge_arr3
  judge_arr5 = PressedChar(c) + judge_arr4
  judge_arr6 = PressedChar(b) + judge_arr5
  judge_arr7 = PressedChar(a) + judge_arr6
  judge_arr8 = PressedChar(k) + judge_arr7
  judge_arr9 = PressedChar(j) + judge_arr8
  judge_arr10 = PressedChar(i) + judge_arr9
}
function GeshihuaI() {
  judge_arr1 = PressedChar(h)
  judge_arr2 = PressedChar(g) + judge_arr1
  judge_arr3 = PressedChar(f) + judge_arr2
  judge_arr4 = PressedChar(e) + judge_arr3
  judge_arr5 = PressedChar(d) + judge_arr4
  judge_arr6 = PressedChar(c) + judge_arr5
  judge_arr7 = PressedChar(b) + judge_arr6
  judge_arr8 = PressedChar(a) + judge_arr7
  judge_arr9 = PressedChar(k) + judge_arr8
  judge_arr10 = PressedChar(g) + judge_arr9
}
function GeshihuaJ() {
  judge_arr1 = PressedChar(i)
  judge_arr2 = PressedChar(h) + judge_arr1
  judge_arr3 = PressedChar(g) + judge_arr2
  judge_arr4 = PressedChar(f) + judge_arr3
  judge_arr5 = PressedChar(e) + judge_arr4
  judge_arr6 = PressedChar(d) + judge_arr5
  judge_arr7 = PressedChar(c) + judge_arr6
  judge_arr8 = PressedChar(b) + judge_arr7
  judge_arr9 = PressedChar(a) + judge_arr8
  judge_arr10 = PressedChar(k) + judge_arr9
}
function GeshihuaK() {
  judge_arr1 = PressedChar(g)
  judge_arr2 = PressedChar(i) + judge_arr1
  judge_arr3 = PressedChar(h) + judge_arr2
  judge_arr4 = PressedChar(g) + judge_arr3
  judge_arr5 = PressedChar(f) + judge_arr4
  judge_arr6 = PressedChar(e) + judge_arr5
  judge_arr7 = PressedChar(d) + judge_arr6
  judge_arr8 = PressedChar(c) + judge_arr7
  judge_arr9 = PressedChar(b) + judge_arr8
  judge_arr10 = PressedChar(a) + judge_arr9
}
function PressedChar(a) {
  let judge_arr1 = undefined
  if (a >= 65 && a <= 90) {
    judge_arr1 = String.fromCharCode(a + 32)
  }
  if (a == 48) {
    judge_arr1 = ")"
  }
  if (a == 49) {
    judge_arr1 = "!"
  }
  if (a == 50) {
    judge_arr1 = "@"
  }
  if (a == 51) {
    judge_arr1 = "#"
  }
  if (a == 52) {
    judge_arr1 = "$"
  }
  if (a == 53) {
    judge_arr1 = "%"
  }
  if (a == 54) {
    judge_arr1 = "^"
  }
  if (a == 56) {
    judge_arr1 = "*"
  }
  if (a == 57) {
    judge_arr1 = "("
  }
  if (a == 186) {
    judge_arr1 = ":"
  }
  if (a == 187) {
    judge_arr1 = "+"
  }
  if (a == 188) {
    judge_arr1 = "<"
  }
  if (a == 190) {
    judge_arr1 = ">"
  }
  if (a == 191) {
    judge_arr1 = "?"
  }
  if (a == 192) {
    judge_arr1 = "~"
  }
  if (a == 219) {
    judge_arr1 = "{"
  }
  if (a == 220) {
    judge_arr1 = "|"
  }
  if (a == 221) {
    judge_arr1 = "}"
  }
  if (a == 222) {
    judge_arr1 = '""'
  }
  return judge_arr1
}



// var app = electron.app   // 创建electron引用

// var globalShortcut = electron.globalShortcut


// var BrowserWindow = electron.BrowserWindow;  //创建窗口引用

// var mainWindow = null;  //声明要打开的主窗口
// app.on('ready', () => {
//   require('./main/menu.js')
//   mainWindow = new BrowserWindow({
//     width: 400, height: 400,//设置打开的窗口大小
//     webPreferences: { nodeIntegration: true }//node下所有内容可以在渲染进程中使用
//   })

//   globalShortcut.register('*', () => {//注册全局事件
//     // mainWindow.loadURL('https://jspang.com')
//     robot.typeString("Hello World");
//     // console.log(1)
//   })

//   mainWindow.loadFile('demo2.html')  //加载那个页面

//   //监听关闭事件，把主窗口设置为null
//   mainWindow.on('closed', () => {
//     mainWindow = null
//   })

// })
